package lucky.xiong.api.Stream流;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author XiongJianLin
 * @date Created in 2022/2/10 15:03
 */
public class GroupBy {
    public static void main(String[] args) {
        List<Sip> sips = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Sip s = new Sip();
            s.setId(1);
            s.setValue("账号"+Math.random()*100);
            sips.add(s);
        }
        for (int i = 0; i < 3; i++) {
            Sip s = new Sip();
            s.setId(2);
            s.setValue("账号"+Math.random()*100);
            sips.add(s);
        }
        for (int i = 0; i < 4; i++) {
            Sip s = new Sip();
            s.setId(3);
            s.setValue("账号"+Math.random()*100);
            sips.add(s);
        }

        System.out.println(sips);

        Map<Integer,List<Sip>> group = sips.stream().collect(
                Collectors.groupingBy(
                        Sip::getId,Collectors.toList()
                ));
        System.out.println(group);
    }



    static class Sip{
        private int id;
        private String value;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("{");
            sb.append("\"id\":")
                    .append(id);
            sb.append(",\"value\":\"")
                    .append(value).append('\"');
            sb.append('}');
            return sb.toString();
        }
    }
}
